using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using Unity.Collections.LowLevel.Unsafe;
using System;

public class CamaraControl : MonoBehaviour {
    
    [Header("事件监听")]
    public VoidEventSO afterSceneLoadedEvent;
    public VoidEventSO camaraShakeEvent;
    
    private CinemachineConfiner2D confiner2D;
    public CinemachineImpulseSource impulseSource;

    private void Awake() {
        confiner2D = GetComponent<CinemachineConfiner2D>();
    }

    private void OnEnable() {
        camaraShakeEvent.OnEventRaised += OnCamaraShake;
        afterSceneLoadedEvent.OnEventRaised += OnAfterSceneLoaded;
    }

    private void OnDisable() {
        camaraShakeEvent.OnEventRaised -= OnCamaraShake;
        afterSceneLoadedEvent.OnEventRaised -= OnAfterSceneLoaded;
    }
    
    private void Start() {
        GetNewCamaraBounds();
    }

    private void GetNewCamaraBounds() {
        var obj = GameObject.FindGameObjectWithTag("Bounds");
        if (obj == null) {
            return;
        }

        confiner2D.m_BoundingShape2D = obj.GetComponent<Collider2D>();

        confiner2D.InvalidateCache();
    }

    private void OnCamaraShake() {
        impulseSource.GenerateImpulse();
    }

    private void OnAfterSceneLoaded() {
        GetNewCamaraBounds();
    }

}